//
//  YTCarInfoViewModel.m
//  YiTuDriverClient
//
//  Created by 石少庸 on 2017/7/14.
//  Copyright © 2017年 Yitu. All rights reserved.
//

#import "YTCarInfoViewModel.h"
#import "YTDriverCarServices.h"
#import "YTCarDetailModel.h"

@interface YTCarInfoViewModel ()
@property (strong, nonatomic, readwrite) RACCommand *submitCarInfo1;
@property (strong, nonatomic, readwrite) RACCommand *submitCarInfo2;
@property (strong, nonatomic, readwrite) RACCommand *loadCarInfo;
@property (strong, nonatomic, readwrite) RACCommand *changeCarState;
@end

@implementation YTCarInfoViewModel
- (void)initialize {
    [super initialize];
    // 加载车辆详情
    _loadCarInfo = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(NSString *car_id) {
        return [[YTDriverCarServices carDetailWithID:car_id] map:^id(NSDictionary *response) {
            return [YTCarDetailModel yy_modelWithJSON:response[@"responseData"]];
        }];
    }];
    
    // 开启或关闭使用状态
    _changeCarState = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(RACTuple *data) {
        NSMutableDictionary *dict = [NSMutableDictionary dictionary];
        dict[@"carId"] = data.first;
        dict[@"status"] = data.second;
        return [[YTDriverCarServices activeCar:dict] map:^id(NSDictionary *response) {
            return @([response[@"resultCode"] integerValue] == 1000);
        }];
    }];
    
    // 前面三张图片更新
    id (^imageNotEmpty)(UIImage *image) = ^id(UIImage *image) { // 图片非空
        return @(image != nil);
    };
    RACSignal *signal1 = [RACObserve(self, image1) map:imageNotEmpty];
    RACSignal *signal2 = [RACObserve(self, image2) map:imageNotEmpty];
    RACSignal *signal3 = [RACObserve(self, image3) map:imageNotEmpty];
    RACSignal *enable1 = [RACSignal combineLatest:@[signal1, signal2, signal3]
                                           reduce:^id (NSNumber *not1, NSNumber *not2, NSNumber *not3) {
                                               return @(not1.boolValue && not2.boolValue && not3.boolValue);
    }];
    @weakify(self);
    _submitCarInfo1 = [[RACCommand alloc] initWithEnabled:enable1 signalBlock:^RACSignal *(id input) {
        @strongify(self);
        NSMutableDictionary *dict = [NSMutableDictionary dictionary];
        dict[@"carId"] = self.car_id;
        dict[@"carImage1"] = [UIImage yt_imageToString:self.image1];
        dict[@"carImage2"] = [UIImage yt_imageToString:self.image2];
        dict[@"carImage3"] = [UIImage yt_imageToString:self.image3];
        return [[YTDriverCarServices updateCarInfo:dict] map:^id(NSDictionary *response) {
            return @(([response[@"resultCode"] integerValue] == 1000));
        }];
    }];
    
    // 驾照,行驶证,保险信息更新
    id (^selectedDate)(NSString *str) = ^id(NSString *str) { // 非空
        return @(str.length > 5);
    };
    RACSignal *driverDate = [RACObserve(self, carDrivingLicenseTime) map:selectedDate];
    RACSignal *inspectDate = [RACObserve(self, carInspectexpTime) map:selectedDate];
    RACSignal *insuranceDate = [RACObserve(self, carInsuranceTime) map:selectedDate];
    RACSignal *driverImage = [RACObserve(self, drivingLicenseImage) map:imageNotEmpty];
    RACSignal *inspectImage = [RACObserve(self, carInspectexpImage) map:imageNotEmpty];
    RACSignal *insuranceImage = [RACObserve(self, insuranceImage) map:imageNotEmpty];
    RACSignal *enable2 = [RACSignal combineLatest:@[driverDate, inspectDate, insuranceDate,
                                                         driverImage, inspectImage, insuranceImage]
                                                reduce:^id(NSNumber *driver, NSNumber *inspect, NSNumber *insurance,
                                                           NSNumber *image1, NSNumber *image2, NSNumber *image3) {
                                                    return @(driver.boolValue && inspect.boolValue && insurance.boolValue && image1.boolValue && image2.boolValue && image3.boolValue);
                                                }];
    _submitCarInfo2 = [[RACCommand alloc] initWithEnabled:enable2 signalBlock:^RACSignal *(id input) {
        @strongify(self);
        NSMutableDictionary *dict = [NSMutableDictionary dictionary];
        dict[@"carId"] = self.car_id;
        dict[@"drivingLicenseImage"] = [UIImage yt_imageToString:self.drivingLicenseImage];
        dict[@"carInspectexpImage"] = [UIImage yt_imageToString:self.carInspectexpImage];
        dict[@"insuranceImage"] = [UIImage yt_imageToString:self.insuranceImage];
        dict[@"carDrivingLicenseTime"] = self.carDrivingLicenseTime;
        dict[@"carInspectexpTime"] = self.carInspectexpTime;
        dict[@"carInsexpTime"] = self.carInsuranceTime;
        return [YTDriverCarServices updateCarInfo:dict];
    }];
}
@end
